<!DOCTYPE html>
<html>
<head>
  <title>TensorFlow.js Linear Regression Example</title>
  <!-- Load TensorFlow.js -->
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
</head>
<body>

<script>
    
  async function run() {
    // 创建一个简单的线性模型 y = wx + b
    const model = tf.sequential();
    model.add(tf.layers.dense({units: 1, inputShape: [1]}));

    // 编译模型，设置损失函数和优化器
    model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

    // 训练数据
    const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
    const ys = tf.tensor2d([2, 4, 6, 8], [4, 1]);

    // 训练模型
    await model.fit(xs, ys, {epochs: 100});

    // 预测新数据点
    const prediction = model.predict(tf.tensor2d([5], [1, 1]));
    prediction.print();
  }

  run();
</script>

</body>
</html>